﻿using System;
using System.Collections.Generic;
using DbConnect.Sql;
using DbConnect;
namespace DnDEngine.Class
{
    public class RespType
    {
        #region "Private attributes"


        #endregion

        #region "Properties"

        public int ID
        {
            get;
            set;
        }

        public String Lib
        {
            get;
            set;
        }

        #endregion

        #region "Events"

        #endregion

        #region "Public functions"

        public RespType(String RespTypeLib)
        {

            try
            {
                Logger.Log.TraceAction("RespType.Constructor", RespTypeLib);
                Lib = RespTypeLib;
                Insert();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("RespType.Constructor", ex);

            }
            finally
            {
            }

        }

        public RespType(int IDRespType)
        {

            try
            {
                Logger.Log.TraceAction("RespType.Constructor", IDRespType.ToString());
                ID = IDRespType;
                Load();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("RespType.Constructor", ex);

            }
            finally
            {
            }

        }

        #endregion

        #region "Private functions"

        private int Insert()
        {

            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("RespType.Insert", "");
                var data = new DbConnect.DataAccess();
                var req = SqlRespType.InsertInto;
                data.AddParam(ParamRespType.Lib, this.Lib);

                Ok = data.Insert(req);

                this.ID = data.GetLastIDInserted(SqlRespType.TableName);
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("RespType.Insert", ex);
                Ok = 0;
            }
            finally
            {
            }
            return Ok;
        }

        private void Load()
        {


            try
            {
                Logger.Log.TraceAction("RespType.Load", "");
                var data = new DbConnect.DataAccess();
                data.AddParam(ParamRespType.ID, this.ID);
                var req = SqlRequest.Select + SqlRespType.RespTypeLib + SqlRequest.From + SqlRespType.TableName +
                    SqlRequest.Where + SqlRespType.ID + SqlRequest.Equal + this.ID;
                var table = data.Select(req);

                if (table != null && table.Rows.Count > 0)
                {
                    Lib = table.Rows[0].GetStringValue("RespTypeLib");
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("RespType.Load", ex);

            }
            finally
            {
            }

        }

        private int Delete()
        {

            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("RespType.Insert", "");
                var data = new DbConnect.DataAccess();
                var req = SqlRespType.Delete;
                data.AddParam(ParamRespType.ID, ID);

                Ok = data.Delete(req);
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("RespType.Insert", ex);
                Ok = 0;
            }
            finally
            {
            }
            return Ok;
        }

        private int Update()
        {

            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("RespType.Insert", "");
                var data = new DbConnect.DataAccess();
                var req = SqlRespType.Update;
                data.AddParam(ParamRespType.ID, ID);
                data.AddParam(ParamRespType.Lib, Lib);

                Ok = data.Update(req);
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("RespType.Insert", ex);
                Ok = 0;
            }
            finally
            {
            }
            return Ok;
        }

        #endregion


    }
}
